package com.tdr.myandroid.db;

import android.database.Cursor;

import com.tdr.myandroid.model.EventObject;

import java.util.ArrayList;

/**
 * Created by Administrator on 2014/7/16.
 */
public class EventsDao {
    private ArrayList<EventObject> eventsList = new ArrayList<EventObject>();

    public ArrayList<EventObject> getEventsList() {
        return eventsList;
    }

    public void updateAllEvents() {
        eventsList.clear();

        Cursor cur = DatabaseHelper.getDbHelper().getReadableDatabase().rawQuery("select * from events", null);

        while (cur.moveToNext()) {
            int id = cur.getInt(cur.getColumnIndex("id"));
            String contentString = cur.getString(cur.getColumnIndex("content"));
            String dateString = cur.getString(cur.getColumnIndex("update_date"));
            EventObject newEvent = new EventObject("","");
            newEvent.setId(id);
            newEvent.setContentString(contentString);
            newEvent.setDateString(dateString);
            eventsList.add(newEvent);
        }
    }

    public EventObject getEventById(int id ) {
        EventObject obj = null;

        for (EventObject singleEvent : eventsList) {
            if (singleEvent.getId() == id) {
                obj = singleEvent;
                break;
            }
        }
        return obj;
    }
}
